package com.sdp.wz.controller;


import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wz.db.wz_info.entity.WzInfo;
import com.sdp.wz.db.wz_info.service.IWzInfoService;
import com.sdp.wz.db.wz_input.entity.WzInput;
import com.sdp.wz.db.wz_input.service.IWzInputService;
import com.sdp.wz.db.wz_input_detail.entity.WzInputDetail;
import com.sdp.wz.db.wz_input_detail.service.IWzInputDetailService;
import com.sdp.wz.db.wz_serial_code.service.IWzSerialCodeService;
import com.sdp.wz.db.wz_vendor_wz.entity.WzVendorWz;
import com.sdp.wz.db.wz_vendor_wz.service.IWzVendorWzService;
import com.sdp.wz.db.wz_warehouse_info.entity.WzWarehouseInfo;
import com.sdp.wz.db.wz_warehouse_info.service.IWzWarehouseInfoService;
import com.sdp.wz.handleImpl.WzDirectInputHandleImpl;
import com.sdp.wz.utils.WzConstants;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/WzInputController")
public class WzInputController {
	
	@Autowired
	private Query query;
	
	@Autowired
	private IWzInputService iWzInputService;
	
	@Autowired
	private IWzSerialCodeService iWzSerialCodeService;
	
	@Autowired
	private IWzInputDetailService iWzInputDetailService;
	
	@Autowired
	private WzDirectInputHandleImpl wzDirectInputHandleImpl;
	
	@Autowired
	private IWzWarehouseInfoService iWzWarehouseInfoService;
	
	@Autowired
	private IWzVendorWzService iWzVendorWzService;
	
	@Autowired
	private IWzInfoService iWzInfoService;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWzInputService, WzInput.class, dataCenter));
	}
	
	/**
	 * 添加物资入库单信息，此时还不能添加入库的物资信息，只有先保存入库单，形成单据才能添加物资
	 * 因为牵扯到数据导入
	 * @param input
	 * @return
	 */
	@PostMapping(value = "/addWzInput")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> addWzInput(@RequestBody WzInput wzInput) {
		if(wzInput==null) {
			return Result.error("入库信息为空");
		}
		if(StringUtils.isEmpty(wzInput.getWarehouseCode())) {
			return Result.error("仓库信息为空");
		}
		if(StringUtils.isEmpty(wzInput.getKeyId())) {
			String serialCode = iWzSerialCodeService.createSerialCode(WzConstants.流水号前缀_物资入库单.getCode());
			if(StringUtils.isEmpty(serialCode)) {
				return Result.error("入库单编号生成失败，请稍后再试");
			}
			WzWarehouseInfo warehouse = iWzWarehouseInfoService.queryWarehouseInfoByCode(wzInput.getWarehouseCode());
			if(warehouse==null) {
				return Result.error("仓库不存在");
			}
			wzInput.setIsUse(WzConstants.否.getCode());
			wzInput.setIsOutput(WzConstants.否.getCode());
			wzInput.setOrgNo(warehouse.getOrgNo());
			wzInput.setCreateTime(LocalDateTime.now());
			wzInput.setInputCode(serialCode);
			wzInput.setInputDate(LocalDate.now());
			wzInput.setInputTime(LocalDateTime.now());
			iWzInputService.save(wzInput);
			
			//根据仓库和供应商，初始化物资信息
			List<WzVendorWz> vendorWzList = iWzVendorWzService.queryWzVendorWzByvendorAndWarehouse(wzInput.getWarehouseCode(), wzInput.getVendorCode());
			if(vendorWzList!=null && vendorWzList.size()>0) {
				List<String> wzNos = new ArrayList<>();
				for(WzVendorWz w : vendorWzList) {
					wzNos.add(w.getWzNo());
				}
				
				List<WzInputDetail> detailList = new ArrayList<>();
				WzInputDetail detail = null;
				//根据物资编码查询物资信息
				List<WzInfo> wzInfoList = iWzInfoService.queryWzInfoByWzNos(wzNos);
				for(WzInfo wz : wzInfoList) {
					detail = new WzInputDetail();
					detail.setInputCode(serialCode);
					detail.setWzNo(wz.getWzNo());
					detail.setWzName(wz.getWzName());
					detail.setOrgNo(wzInput.getOrgNo());
					detail.setInputNum(BigDecimal.ZERO);
					detail.setCreateTime(LocalDateTime.now());
					detail.setVendorCode(wzInput.getVendorCode());
					detail.setWzUnit(wz.getWzUnit());
					detail.setWzModel(wz.getWzModel());
					detailList.add(detail);
				}
				
				iWzInputDetailService.saveBatch(detailList);
			}
		}else {
			iWzInputService.updateById(wzInput);
		}
		return Result.ok(wzInput);
	}
	
	
	/**
	 * 入库的物资信息保存，未提交，不影响库存
	 * @param input
	 * @return
	 */
	@PostMapping(value = "/saveWzList")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody List<WzInputDetail> wzList) {
		if(wzList==null) {
			return Result.error("入库的物资信息为空");
		}
		if(wzList!=null && wzList.size()>0) {
			
			iWzInputDetailService.saveOrUpdateBatch(wzList);
		}
		return Result.ok();
	}
	
	
	@PostMapping(value = "/commit")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> commit(@RequestBody JSONObject input) {
		if(input==null) {
			return Result.error("入库信息为空");
		}
		WzInput wzInput = input.getObject("wzInput", WzInput.class);
		
		if(wzInput==null) {
			return Result.error("入库信息为空");
		}
		if(StringUtils.isEmpty(wzInput.getKeyId())) {
			return Result.error("入库数据异常");
		}
		List<WzInputDetail> wzList = JSONArray.parseArray(JSON.toJSONString(input.get("wzInputDetail")), WzInputDetail.class);
		if(wzList==null || wzList.size()==0) {
			return Result.error("入库的物资信息为空");
		}
		BusinessContext businessContext = new BusinessContext();
		businessContext.put(WzInput.class, wzInput);
		businessContext.put(WzInputDetail.class, wzList);
		wzDirectInputHandleImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(wzInput);
		}else {
			return Result.error(businessContext.getMessage());
		}
	}
}

